package cn.tedu.csmall.stock.webapi.quartz;

import cn.tedu.csmall.commons.pojo.stock.entity.Stock;
import cn.tedu.csmall.stock.webapi.quartz.config.RabbitMQConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.RandomUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;

@Slf4j
public class QuartzJob implements Job {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    static int x = 1;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("-----------------{}-----------------", LocalDateTime.now());
//        System.out.println(x++);
        Stock stock = new Stock();
        stock.setId(x++);
        stock.setCommodityCode("PC100");
        stock.setReduceCount(1 + RandomUtils.nextInt(20));
        rabbitTemplate.convertAndSend(RabbitMQConfig.STOCK_EX,RabbitMQConfig.STOCK_ROUT,stock);
        log.info("发送消息完成:{}", stock);
    }
}
